package com.example.myjava;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity  extends Activity {
    /** Called when the activity is first created. */
    public static Context instance;
    RelativeLayout mRelativeLayout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        instance=this;
        mRelativeLayout=(RelativeLayout) findViewById(R.id.layoutRacingNeedForSpeed);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        final int screenWidth = dm.widthPixels;

        int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        mRelativeLayout.measure(w, h);
        int height =mRelativeLayout.getMeasuredHeight();
      //  int width =mRelativeLayout.getMeasuredWidth();
        final int screenHeight = height;
        //拖动的按钮
        final Button mstartBtn=(Button)findViewById(R.id.startBtn);
        mstartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                String pkg="com.tencent.mm";
//                String cls="com.tencent.mm.ui.LauncherUI";
//                ComponentName componet = new ComponentName(pkg, cls);
//                //pkg 就是第三方应用的包名
//                //cls 就是第三方应用的进入的第一个Activity
//                Intent intent = new Intent();
//                intent.setAction(Intent.ACTION_VIEW);
//                //   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                startActivity(intent);
//                if(isWeixinAvilible(MainActivity.this)) {
//                    Intent intent = getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
//                    startActivity(intent);
//                }else{
//                    Toast.makeText(MainActivity.this, "您还没有安装微信，请先安装微信客户端",Toast.LENGTH_SHORT).show();
//                }
            }
        });
        //添加触摸事件

        mstartBtn.setOnTouchListener(new View.OnTouchListener(){
            int lastX, lastY; //记录移动的最后的位置
            public boolean onTouch(View v, MotionEvent event) {
                //获取Action

                int ea=event.getAction();

                Log.i("TAG", "Touch:"+ea);
                switch(ea){
                    case MotionEvent.ACTION_DOWN:   //按下
                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        break;
                    /**
                     * layout(l,t,r,b)
                     * l  Left position, relative to parent
                     t  Top position, relative to parent
                     r  Right position, relative to parent
                     b  Bottom position, relative to parent
                     * */
                    case MotionEvent.ACTION_MOVE:  //移动
                        //移动中动态设置位置
                        int dx =(int)event.getRawX() - lastX;
                        int dy =(int)event.getRawY() - lastY;
                        int left = v.getLeft() + dx;
                        int top = v.getTop() + dy;
                        int right = v.getRight() + dx;
                        int bottom = v.getBottom() + dy;
                        if(left < 0){
                            left = 0;
                            right = left + v.getWidth();
                        }
                        if(right > screenWidth){
                            right = screenWidth;
                            left = right - v.getWidth();
                        }
                        if(top < 0){
                            top = 0;
                            bottom = top + v.getHeight();
                        }
                        if(bottom > screenHeight){
                            bottom = screenHeight;
                            top = bottom - v.getHeight();
                        }
                        v.layout(left, top, right, bottom);
                        Log.i("", "position：" + left +", " + top + ", " + right + ", " + bottom);
                        //将当前的位置再次设置
                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:   //脱离
                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        Log.e("lastX",lastX+"==="+lastY);
                        break;
                }
                return false;
            }});
    }
    /**
     * 判断 用户是否安装微信客户端
     */
    public static boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        return false;
    }

//    ViewPager pager = (ViewPager)findViewById(R.id.pager);
//    ViewPagerIndicator indicator = (ViewPagerIndicator) findViewById(R.id.pager_indicator);
//
//    ArrayList<String> list = new ArrayList<>();
//    list.add("page 1");
//    list.add("page 2");
//
//    SimplePagerAdapter adapter = new SimplePagerAdapter(this, list);
//    pager.setAdapter(adapter);
//
//    indicator.setPager(pager);
//
//    list.add("page 3");
//
//    adapter.notifyDataSetChanged();
//
//    //indicator.setPager(pager);
//
//    pager.setCurrentItem(1);
}

